/*
 * Copyright (c) 2006-2009, Petr Panteleyev
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice, 
 *       this list of conditions and the following disclaimer in the documentation 
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products 
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.livejournal;

import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;

import org.panteleyev.blogclient.Icon;
import org.panteleyev.ljapi.LJFriendChange;
import org.panteleyev.ljapi.LJJournalType;
import org.panteleyev.swing.BasicDialog;
import org.panteleyev.swing.TableUtil;

/**
 *
 * @author  Petr Panteleyev
 */
public class FriendListChangeDialog extends BasicDialog<JFrame> {
    private static String[] _changeStrings =  {
        "",
        " - added you",
        " - removed you",
        " - you added",
        " - you removed",
        " - added each other",
        " - removed each other",
        " - journal restored",
        " - journal deleted",
        " - journal suspended",
        " - journal purged",
    };
    
    private ArrayList<LJFriendChange> _change;
    private AbstractTableModel _tableModel;
    
    public FriendListChangeDialog(JFrame parent, ArrayList<LJFriendChange> change) {
        super(parent);
        _change = change;
        initComponents();
        createTableModel();
        changeTable.setTableHeader(null);
        TableUtil.adjustTableColumnSizes(changeTable);
        java.awt.Dimension size = changeTable.getPreferredSize();
        if (size.getHeight() > 300) {
            size.setSize(size.getWidth(), 300);
        }
        changeTable.setPreferredScrollableViewportSize(size);
        pack();        
    }
    
    private void createTableModel() {
        _tableModel = new DefaultTableModel() {
            @Override
            public int getColumnCount() { return 4; }
            
            @Override
            public int getRowCount() { return _change.size(); }

            @Override
            public Class<?> getColumnClass(int col) {
                return (col == 0)? ImageIcon.class : String.class;
            }
      
            @Override
            public Object getValueAt(int row, int col) {
                LJFriendChange c = _change.get(row);
                switch (col) {
                    case 0: {
                        switch (c.getJournalType()) {
                            case USER:
                                return Icon.FRIEND_HEAD.i();
                            case COMMUNITY:
                                return Icon.COMMUNITY_ICON.i();
                            case SYNDICATED:
                                return Icon.SYNDICATED.i();
                            case IDENTITY:
                                switch (c.getIdentityType()) {
                                    case OPENID:
                                        return Icon.OPENID.i();
                                    case FACEBOOK:
                                        return Icon.FACEBOOK.i();
                                    case TWITTER:
                                        return Icon.TWITTER.i();
                                    default:
                                        return null;
                                }
                            default:
                                return null;
                        }
                    }
                    case 1:
                        if (c.getJournalType() == LJJournalType.IDENTITY) {
                            return c.getIdentityDisplay();
                        } else {
                            return c.getName();
                        }
                    case 2:
                        return c.getFullName();
                    case 3:
                        return _changeStrings[c.getType().ordinal()];
                    default:
                        return 0;
                }
            }            
        };
        
        changeTable.setModel(_tableModel);

/*
        FriendTypeTableCellRenderer renderer = new FriendTypeTableCellRenderer();
        DefaultTableCellRenderer defRenderer = new DefaultTableCellRenderer();
        
        for (int i = 0; i < _tableModel.getColumnCount(); i++) {
            TableColumn column = new TableColumn(i, 10, (i == 0)? renderer : defRenderer, null);
            changeTable.addColumn(column);            
        }
 */
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        topPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        changeTable = new javax.swing.JTable();
        buttonPanel = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(L10N.FRIEND_LIST_CHANGE_DIALOG_TITLE.s());
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        topPanel.setPreferredSize(null);
        topPanel.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setPreferredSize(null);

        changeTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        changeTable.setPreferredSize(null);
        changeTable.setShowHorizontalLines(false);
        changeTable.setShowVerticalLines(false);
        changeTable.setTableHeader(null);
        jScrollPane1.setViewportView(changeTable);

        topPanel.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(topPanel, gridBagConstraints);

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(okButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        getContentPane().add(buttonPanel, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        onOk();
    }//GEN-LAST:event_okButtonActionPerformed
        
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JTable changeTable;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton okButton;
    private javax.swing.JPanel topPanel;
    // End of variables declaration//GEN-END:variables
    
}
